///  <copyright file="MallowMain.cs" company="Infozoom" author="Stefan Zschocke">
///    You must not remove this notice, or any other, from this software.
///    Otherwise do with it whatever you want.
///    infoZoom may not be made liable for any damages whatsoever 
///    (including, without limitation, damages for loss of business profits, 
///    business interruption, loss of business information or other pecuniary loss)
///    arising out of use of or inability to use the Software
/// </copyright>
using System.Collections;
using System.Xml;

namespace Mallow
{
    /// <summary>
    /// Summary description for Component.
    /// </summary>
    internal class Component
    {
        internal Component(Mallow mallow, Directory parent, XmlNode node, bool isNew)
        {
            mMallow = mallow;
            mParent = parent;
            mNode = node;
            mNew = isNew;
            if (!isNew) AddFiles();
        }

        internal XmlNode Node { get { return mNode; } }
        internal bool IsNew { get { return mNew; } set { mNew = value; } }
        internal Directory Directory { get { return mParent; } }
        internal bool FindFile(System.IO.FileInfo fileInfo)
        {
            foreach (File file in mFiles)
            {
                if (Directory.FileNameComparer.Compare(file.FileName, fileInfo.Name) == 0)
                {
                    file.Found = true;
                    return true;
                }
            }
            return false;
        }

        internal void AddNewFile(XmlNode node)
        {
            mNode.AppendChild(node);
            mFiles.Add(new File(mMallow, this, node, true));
        }

        void AddFiles()
        {
            foreach (XmlNode child in mNode.ChildNodes)
            {
                if (child.Name.Equals("File")) mFiles.Add(new File(mMallow, this, child, false));
            }
        }

        internal void Synch(ProtocolBuilder protocol, out bool needed)
        {
            needed = false;
            bool changed = false;
            foreach (File file in mFiles)
            {
                if (file.Found)
                {
                    needed = true;
                    if (file.IsNew)
                    {
                        protocol.FileAdded(file);
                    }
                }
                else
                {
                    protocol.FileRemoved(file);
                    mNode.RemoveChild(file.Node);
                }
                if (!IsNew)
                {
                    if (file.IsNew || !file.Found) changed = true;
                }
            }
            if (!needed)
            {
                protocol.ComponentRemoved(this);
            }
            else if (IsNew)
            {
                protocol.ComponentAdded(this);
            }
            else if (changed)
            {
                protocol.ComponentChanged(this);
            }
            else
            {
                protocol.ComponentUnchanged(this);
            }
        }

        ArrayList mFiles = new ArrayList();
        XmlNode mNode;
        Directory mParent;
        Mallow mMallow;
        bool mNew;
    }
}
